package com.bfl.sa.sys.user.service;

import com.bfl.sa.common.service.BaseService;
import com.bfl.sa.sys.user.entity.UserLastOnline;
import com.bfl.sa.sys.user.mapper.UserLastOnlineMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;


@Service
public class UserLastOnlineService extends BaseService<UserLastOnline, Long> {

    @Resource
    private UserLastOnlineMapper userLastOnlineMapper;

    public UserLastOnline findByUserId(Long userId) {
        return userLastOnlineMapper.findByUserId(userId);
    }

    public void lastOnline(UserLastOnline lastOnline) {
        UserLastOnline dbLastOnline = findByUserId(lastOnline.getUserId());
        if (dbLastOnline == null) {
            dbLastOnline = lastOnline;
        } else {
            UserLastOnline.merge(lastOnline, dbLastOnline);
        }
        dbLastOnline.incLoginCount();
        dbLastOnline.incTotalOnlineTime();
        //相对于save or update
        save(dbLastOnline);
    }
}
